//index.js
//获取应用实例
var app = getApp()
var admin_id = app.globalData.admin_id;
var goods_name = 0;
var share = app.globalData.share;
var left = 250;
Page({
  data: {
    nav: [

    ],
    array1: [''],
    array2: [''],
    curID: 's1',
    index1: -1,
    index2: -1,
    curAct: 0,
    name: ''
  },
  onLoad: function () {
    var that = this;
    console.log("管理员ID ====" + admin_id);
    app.getlist(66587, function (res) {
      console.log(res)
      let arr = []
      for (var i = 0; i < res.length; i++) {
        arr.push(res[i].name)
      }
      that.setData({
        array1: arr,
        flist: res
      })
    })
    // app.getlist(66589, function (res) {
    //   console.log(res)
    //   let arr = []
    //   for (var i = 0; i < res.length; i++) {
    //     arr.push(res[i].name)
    //   }
    //   that.setData({
    //     array2: arr
    //   })
    // })


    //获取后台人员信息
    // wx.request({
    //   url: 'https://xcxu.we-fs.com/index.php/api/goods/goodsInfo?admin_id=' + app.globalData.admin_id + '&type=' + 66592 + '&code=3',
    //   success: function (res) {
    //     console.log(res.data.data)
    //     that.setData({
    //       list: res.data.data
    //     })
    //   }
    // })
  },
  bindPickerChange1(e) {
    var that = this;
    console.log(e.detail.value)
    this.setData({
      index1: e.detail.value,
      index2: -1,
      array2: [] 
    })

    app.getlist(this.data.flist[e.detail.value].id, function (res) {
      console.log(res)
      let arr = []

      if(res){
        for (var i = 0; i < res.length; i++) {
          arr.push(res[i].name)
        }
        that.setData({
          array2: arr
        })
      }
      
    })
  },
  bindPickerChange2(e) {
    console.log(e.detail.value)
    this.setData({
      index2: e.detail.value
    })
  },
  nameinput(e) {
    this.setData({
      name: e.detail.value
    })
  },
  filterHeaderCatid: function (e) {
    return (e.goods_name == goods_name)
  },

  onShareAppMessage: function () {
    return {
      title: share,
      path: 'pages/index/index'
    }
  },
  subit: function () {
    var that = this;
    console.log('submit')
    if (this.data.index1 == -1) {
      wx.showToast({
        title: '请选择机构',
        icon: "none"
      })
      return false
    }
    if (this.data.index2 == -1) {
      wx.showToast({
        title: '请选择中支/部门',
        icon: "none"
      })
      return false
    }
    if (this.data.name == '') {
      wx.showToast({
        title: '请填写姓名',
        icon: "none"
      })
      return false
    }

    var data = {
      jigou: this.data.array1[this.data.index1],
      bumen: this.data.array2[this.data.index2],
      name: this.data.name,
    }
    console.log(data)

    let obj = []
    goods_name = data.name
    // console.log (that.data.list.filter(that.filterHeaderCatid))
    // obj = that.data.list.filter(that.filterHeaderCatid)
    wx.showLoading({
      title: '查询中..',
    })
    wx.request({
      url: 'https://xcxu.we-fs.com/index.php/Api/goods/goodsInfo2?admin_id[v]=' + admin_id + '&cat_id[v]=66592&f_3[v]=' + data.jigou + '&f_3[e]=like&f_4[v]=' + data.bumen + '&f_4[e]=like&goods_name[v]=' + data.name + '&goods_name[e]=like',
      success:function(res){
        console.log('11',res.data)
        if (res.data.length == 1) {

          let date3 = new Date('2018-09-10'.replace(/-/g, '/')).getTime() - new Date(res.data[0].f_2.replace(/-/g, '/')).getTime()
          let days = Math.floor(date3 / (24 * 3600 * 1000))

          app.globalData.pname = data.name;
          app.globalData.page = res.data[0].f_1;
          app.globalData.pday = days;

          console.log('年龄', app.page, '', app.pday)
          wx.navigateTo({
            url: '../step1/step1',
          })
        } else {
          wx.showToast({
            title: '查询不到该人员，请检查信息是否正确',
            icon: "none"
          })
          return false
        }
      },
      complete:function(){
        wx.hideLoading()
      }
    })

    return false
    
  }
})
